{{extend 'layout.html'}}

<h2>Vulnerabilties for host {{=host_title_maker(host)}}</h2>

{{=XML(add.formModal())}}

<div id="vuln_list">
    <div id="vuln_buttons" class="navbar btn-toolbar">
        <!--<a id="add_vulndata" class="btn btn-small"><i class="icon-plus"></i> Add</a>-->
        {{=XML(add.btn_show())}}
        <a id="delete_vulndata" class="btn btn-small"><i class="icon-remove"></i> Delete</a>
        <a id="reload_vulndata" class="btn btn-small" onclick="javascript:vulntable.fnReloadAjax(); return false;"><i class="icon-refresh"></i> Reload</a>
        <a id="new_vulndata" class="btn btn-small" href="{{=URL('vulns', 'vulndata_add.html')}}", target="_blank"><i class="icon-warning-sign"></i> Create Vulnerability</a>
    </div>

    {{=form}}

    <div id="dialog-add_vulndata">
    </div>
</div>

<script>
var vulntable;

function vulntableDetails ( nTr ) {
    var aData = vulntable.fnGetData( nTr );
    var sOut = "<table class=\"table\">"
    sOut += "<tr><td>Title:</td><td>"+aData[11]+"</td></tr>"
    sOut += "<tr><td>Description:</td><td>"+aData[10]+"</td></tr>"
    sOut += "<tr><td>Exploits:</td><td>"+aData[12]+"</td></tr>"
    sOut += "</table>"

    return sOut;
}

jQuery(document).ready(function() {

    $("#delete_vulndata").click(function(e) {
        e.preventDefault();

        //bootbox.options.onEscape =
        bootbox.confirm('These items will be permanently deleted and cannot be recovered. Are you sure?', "Cancel", "Delete", function(confirmed) {
            if(confirmed) {
                var checks = dt_checkboxer_select_DT_RowIds(vulntable);
                if (checks == '') { alert('Nothing selected'); return false; }
                var data = "ids=" + checks;
                url = "{{=URL('vulns', 'service_vulns_delete.json', user_signature=True)}}";
                $.web2py.ajax_page( 'POST', url, data, 'undefined' );
            }
        });
    });

    vulntable = jQuery('#vulntable').dataTable( {
        "sDom": 'T<"clear">lfrtip',
        "fnDrawCallback": function() {
            jQuery('input[id="exploited"]').click( function() {
                $(this).parent().parent().find("input[type=checkbox][name=id]").attr("checked", false);
                $(this).parent().parent().removeClass('DTTT_selected');
                url = "{{=URL('vulns', 'service_vuln_exploited.json', user_signature=True)}}";
                $.web2py.ajax_page( 'POST', url, 'ids='+$(this).val(), 'undefined' );
            } );
        },
        "oTableTools": {
            "sRowSelect": "multi",
            "sSelectedClass": "row_selected",
            "sSwfPath": "{{=URL('static', 'TableTools/swf/copy_csv_xls.swf')}}",
            "aButtons": [
                "select_all",
                "select_none",
                "copy",
                "csv"
            ]
        },
        "aoColumns": [
            /* MoreInfo */  null,
            /* ID */        null,
            /* Pwned */     null,
            /* Port */      null,
            /* NXID */      null,
            /* Severity */  { "asSorting": [ "desc", "asc" ] },
            /* CVSSScore */ null,
            /* Status */    null,
            /* Exploit */   null,
            /* Proof */     null,
            /* Description */ { "bVisible": false },
            /* Title */     { "bVisible": false },
            /* Exploits */  { "bVisible": false },
        ],
        "aaSorting": [[ 5, "desc" ]],
        "sAjaxSource": "{{=URL('vulndata_by_host.json', args=request.args(0))}}",
    } );

    jQuery('#vulntable tbody td img').live( 'click', function () {
        var nTr = this.parentNode.parentNode;
        if ( this.src.match('details_close') ) {
            /* This row is already open - close it */
            this.src = "{{=URL('static','images/details_open.png')}}";
            vulntable.fnClose( nTr );
        } else {
            /* Open this row */
            this.src = "{{=URL('static','images/details_close.png')}}";
            vulntable.fnOpen( nTr, vulntableDetails(nTr), 'details' );
        }
        $(nTr).removeClass('row_selected');
    } );

} );
</script>
